
$(function(){
	setTimeout(function(){
		for(var  i  =0;i<config.length ;i++){
			try{
				eval('(init_'+ config[i]+'( config[i] )' +')');				
			}catch(e){
				log(e);
			}
		}
	}, 50);
})

function init_echart(obj){
	var selectors = $(".jui-"+obj);
	if(obj != 'echart' ){ 	//手动调用加载
		selectors = $(obj);
	}
	for(var i=0; i< selectors.length; i++){
		var object = getConfig(selectors.eq(i));
		commonAjax( gdv(object.method,"POST") , 
				localUrl + format_property(object.url) , ( (object.method == 'GET')?'': (  objecter( gdv(object.data,{}),'$') )  ) ,function(data){
			if(data.status == status_code.OK){
				var dataTemp ;
				if(validater.empty(data.data)){
					return ;
				} 
				if( format_property(object.url).indexOf("paged")>-1  ){
					dataTemp = data.data.data ;					
				}else{
					dataTemp = data.data ;
				}
				var x_value =[];
				var y_value =[];
				for(var i=0;i< dataTemp.length; i++){
					x_value.push(dataTemp[dataTemp.length-i-1][object.x] );
					y_value.push(dataTemp[dataTemp.length-i-1][object.y])
				}
				object.x=x_value;
				object.y=y_value;
				if(!validater.empty(object.beforeEchart)){
					object = eval('('+ object.beforeEchart+'(object,dataTemp)' +')');
				}
				echartInit(object);
				if(!validater.empty(object.success) ){
					object.success();					
				}
			}
		});
	}
}

function init_simple_table(obj){
	var selectors = $('div[type-option="'+obj+'"],.jui-'+obj);
	for(var i=0;i<selectors.length;i++){
	}
}

function manual_init_table(selector){
	$(selector).tableAdaptor();	
}

function init_table(obj){
	var selectors = $('div[type-option="'+obj+'"],.jui-'+obj);
	if(obj != 'table' ){ 	//手动调用加载
		selectors = $(obj);
	}
	if(selectors.length>0){
		selectors.eq(0).tableAdaptor();	
	}
	if(selectors.length >1){
		console.log("目前单页面仅支持单个表格初始化");
	}
}

function init_time(obj){
	var selectors = $('.jui-'+obj);
	for(var i=0; i<selectors.length; i++ ){
		var className = "time"+getRandom() ;
		selectors.eq(i).addClass( className );
		var param = getDataOptions(selectors.eq(i));
		if(validater.empty(param) ){
			param={};
		}
		var defaultParam = { 
				format:"YYYY-MM-DD hh:mm:ss", 
				isTime:true,
				festival: false,
				isinitVal: true,
				zIndex:99999999
			}
		param = $.extend(defaultParam,param );
		param.dateCell = "."+className;
		$.jeDate("."+className ,param);
		
		$("."+className).val(DateUtil.addDate(new Date(),gdv(param.defaultValue,0)).Format( tom(param.isTime==true ,"yyyy-MM-dd hh:mm:ss","yyyy-MM-dd" )  ) );
	}
}

function init_search_table(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj); 
	for(var i=0 ; i<selectors.length;i++){
		// 获取检索库的框
		var config = getConfig(selectors.eq(i));
		// 提交方法绑定事件
		$(config.submit).data("table_selector",config.table);
		$(config.submit).data("param",config.param);
		$(config.submit).data("parent_obj",selectors.eq(i));
		$(config.submit).click(function(){
			if( $($(this).data("table_selector")).data("searchflag") == true ){
				return ;
			}else{
				$($(this).data("table_selector")).data("searchflag",true); 
			}
			var paraent_objs =  $(this).data("parent_obj").find("[type-name]") ;	
			var param =  objecter( gdv( $(this).data("param"), {} ) ,'$');
			for(var j=0; j< paraent_objs.length;j++){
				if( paraent_objs.eq(j).attr("type-value") ==  'h' ){
					param[paraent_objs.eq(j).attr("type-name")] = paraent_objs.eq(j).html();			
				}else{
					param[paraent_objs.eq(j).attr("type-name")] = paraent_objs.eq(j).val();					
				}
			}
			// 增加before
			if(config.before != null){
				if(! eval('('+ config.before+'()' +')')){
					return ;
				}
			}
			$($(this).data("table_selector")).data("params", param);
			$($(this).data("table_selector")).tableAdaptor();	
		})
	}
}

function init_form_submit(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj);
	for(var i=0; i< selectors.length; i++){
		var config = getConfig(selectors.eq(i));
		$(config.submit).data("config",config);
		$(config.submit).data("parents",selectors.eq(i));
		// 绑定表单的验证
		validate.init(config.validate,selectors.eq(i));
		$(config.submit).click(function(){
			var objthis = this ;
			if( $(objthis).attr("cflag")== true){
				return ;
			}else{
				$(objthis).attr("cflag",true);
			}
			var config = $(this).data("config");
			var parents_obj = $(this).data("parents").find("[type-name]");
			if(! validate.submit(config.validate,$(this).data("parents") )){
				return ;
			}
			var param = objecter( gdv(config.param,{}),'$');
			if(!validater.empty(config.prev_sumit_func)){
				param = eval('('+ config.prev_sumit_func +'(param)' +')');
			}
			for(var j=0; j< parents_obj.length;j++ ){
				if( parents_obj.eq(j).attr("type-value") == 'h' ){
					if( parents_obj.eq(j).attr("type-category") == 'date' ){
						param[parents_obj.eq(j).attr("type-name")] = new Date( parents_obj.eq(j).html() );	
					}else{
						param[parents_obj.eq(j).attr("type-name")] = parents_obj.eq(j).html();	
					}
				}else{
					if( parents_obj.eq(j).attr("type-category") == 'date' ){
						param[parents_obj.eq(j).attr("type-name")] = new Date( parents_obj.eq(j).val() );
					}else{
						param[parents_obj.eq(j).attr("type-name")] = parents_obj.eq(j).val();
					}
				}
			}
			if(config.before != null){
				if(! eval('('+ config.before+'()' +')')){
					return ;
				}
			}
			// 提交数据
			commonAjax(gdv(config.method,"POST") , localUrl+ format_property(config.url), param,function(data){
				$(objthis).attr("cflag",false);
				if(config.success != null){
					eval('('+ config.success+'(data)' +')');
				}
			}, function(){
				$(objthis).attr("cflag",false);
				if(config.error != null){
					eval('('+ config.error+'()' +')');					
				}
			});
		});
	}
}

function manual_form_submit(object){
	var config = getConfig(object);
	$(config.submit).data("config",config);
	$(config.submit).data("parents",object);
	// 绑定表单的验证
	validate.init(config.validate,object);
	$(config.submit).click(function(){
		var config = $(this).data("config");
		var parents_obj = $(this).data("parents").find("[type-name]");
		if(! validate.submit(config.validate,$(this).data("parents") )){
			return ;
		}
		var param = objecter( gdv(config.param,{}),'$');
		if(!validater.empty(config.prev_sumit_func)){
			param = eval('('+ config.prev_sumit_func +'(param)' +')');
		}
		for(var j=0; j< parents_obj.length;j++ ){
			if( parents_obj.eq(j).attr("type-value") == 'h' ){
				if( parents_obj.eq(j).attr("type-category") == 'date' ){
					param[parents_obj.eq(j).attr("type-name")] = new Date( parents_obj.eq(j).html() );	
				}else{
					param[parents_obj.eq(j).attr("type-name")] = parents_obj.eq(j).html();	
				}
			}else{
				if( parents_obj.eq(j).attr("type-category") == 'date' ){
					param[parents_obj.eq(j).attr("type-name")] = new Date( parents_obj.eq(j).val() );
				}else{
					param[parents_obj.eq(j).attr("type-name")] = parents_obj.eq(j).val();
				}
			}
		}
		// 提交数据
		commonAjax(gdv(config.method,"POST") , localUrl+ format_property(config.url), param,function(data){
			if(config.success != null){
				eval('('+ config.success+'(data)' +')');	
			}
		}, function(){
			if(config.error != null){
				eval('('+ config.error+'()' +')');					
			}
		});
	});
}

function init_validate(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj);
	for(var i=0; i< selectors.length; i++){
		var config = getConfig(selectors.eq(i));
		validate.init(config,selectors.eq(i));
		if( !validater.empty(config.submit)){
			$(config.submit).data("config",config);
			$(config.submit).data("parents",selectors.eq(i));
			$(config.submit).click(function(){
				var config =$(this).data("config") ;
				if(! validate.submit(config, $(this).data("parents") )){
					return ;
				}
				if(! validater.empty(config.success) ){
					eval('('+ config.success+'()' +')');	
				}
			});
		}
	}
}

function init_detail_get(obj){
	setTimeout(function() {
		var selectors = $('[type-option="'+obj+'"],.jui-'+obj) ;
		if(obj != 'detail_get' ){ 	//手动调用加载
			selectors = $(obj);
		}
		for(var i=0 ; i < selectors.length;i++){
			var config = getConfig(selectors.eq(i)) ;
			var childs = selectors.eq(i).find("[type-name]");
			getAjax(localUrl+  format_property(config.url), function(data){
				if(data.status == status_code.OK){
					for(var j=0;  j < childs.length;j++){
						if(childs.eq(j).attr("type-value") == 'h' ){
							childs.eq(j).html( data.data[childs.eq(j).attr("type-name")] ) ;
						}else if(childs.eq(j).attr("type-value") == 'ch'){
							childs.eq(j).html( data.data.data[childs.eq(j).attr("type-name")] ) ;
						}
						else{
							childs.eq(j).val( data.data[childs.eq(j).attr("type-name")] ) ;
						}
					}				
				}
				if(config.success != null ){
					eval('('+ config.success+'(data)' +')');
				}
			},function(){
				if(config.error != null){
					eval('('+ config.error+'()' +')');		
				}
			})
		}
	}, 350);
}

/**
 * 手动加载
 * @param object
 * @returns
 */
function manual_init_detail_get(object){
	var config = getConfig(object) ;
	var childs = object.find("[type-name]");
	getAjax(localUrl+  format_property(config.url), function(data){
		if(data.status == status_code.OK){
			for(var j=0;  j < childs.length;j++){
				if(childs.eq(j).attr("type-value") == 'h' ){
					childs.eq(j).html( data.data[childs.eq(j).attr("type-name")] ) ;
				}else{
					childs.eq(j).val( data.data[childs.eq(j).attr("type-name")] ) ;
				}
			}				
		}
		if(config.success != null ){
			eval('('+ config.success+'(data)' +')');
		}
	},function(){
		if(config.error != null){
			eval('('+ config.error+'()' +')');		
		}
	})
}

function init_detail_post(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj) ;
	for(var i=0 ; i < selectors.length;i++){
		var config = getConfig(selectors.eq(i)) ;
		var childs = selectors.eq(i).find("[type-name]");
		postAjax(localUrl+  format_property(config.url),  objecter( gdv(config.param,{}),'$'),  function(data){
			if(data.status == status_code.OK){
				for(var j=0;  j < childs.length;j++){
					if(childs.eq(j).attr("type-value") == 'h' ){
						childs.eq(j).html( data.data[childs.eq(j).attr("type-name")] ) ;
					}else{
						childs.eq(j).val( data.data[childs.eq(j).attr("type-name")] ) ;
					}
				}
			}
			if(config.success != null ){
				eval('('+ config.success+'(data)' +')');
			}
		},function(){
			if(config.error != null){
				eval('('+ config.error+'()' +')');		
			}
		} )
	}
}

function init_selecter(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj) ;
	for(var i=0;i< selectors.length; i++ ){
		var config  = getConfig(selectors.eq(i)) ;
		init_select(config,selectors.eq(i));
	}
}

function manual_init_selecter(object){
	var config  = getConfig(object) ;
	init_select(config,object);
}

function init_auto_complete(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj) ;
	for(var i=0;i< selectors.length; i++ ){
		var config  = getConfig(selectors.eq(i)) ;
		selectors.eq(i).autocomplete({
	        source: function(request, response) {
	        	var data = {} ;
	        	data[config.value] = request.term ;
	        	if(data!=undefined || data!=null){
	        		data = $.extend(data, objecter( gdv(config.param,{}),'$') );
	        	}
		    	var availableTagsJSON;
		    	doAjaxPostData( gdv(config.method,'POST') ,localUrl+ format_property(config.url),data,function(data){
					if(data.status == status_code.OK){
						 availableTagsJSON = data.data.data;
						 response($.map(availableTagsJSON, function(item) {
			                return {
			                    label: item[config.value],
			                    code: item[config.key]
			                }
			            }));
					}
				},function(){
					tip("error");
				})
	        },
	        minLength: gdv(config.tip_length,1) , 
	        select:function(event,ui){
	        	if(! validater.empty(config.success)){
	        		eval('('+ config.success+'(ui)' +')');	        		
	        	}
	        }
	    });
	}
}

function manual_init_template(object){
	var config  = getDataOptions(object) ;
	config.selector = object;
	commonAjax( gdv(config.method,'POST') ,localUrl+format_property(config.url),
			gdv( objecter( gdv(config.param,{}),'$'),{}),function(data){
		if(data.status == status_code.OK){
			config.selector.empty();
			var dataTemp ;
			if(config.type=='page' ){
				dataTemp = data.data.data ;					
			}else{
				dataTemp = data.data ;
			}
			for(var i=0;i<dataTemp.length;i++){
				config.selector.append(	eval('('+ config.reback+'(config.template ,dataTemp[ '+i+' ])' +')')  );
			}
		}
	}, function(){})
}

/**
 * 模板信息初始化
 * @param obj
 * @returns
 */
function init_template(obj){
	var selectors = $(".jui-"+obj) ;
	for(var i=0;i< selectors.length; i++ ){
		var config = getDataOptions(selectors.eq(i));
		config.selector = selectors.eq(i);
		commonAjax( gdv(config.method,'POST') ,localUrl+format_property(config.url),
				gdv( objecter( gdv(config.param,{}),'$'),{}),function(data){
			if(data.status == status_code.OK){
				config.selector.empty();
				var dataTemp ;
				if(config.type=='page' ){
					dataTemp = data.data.data ;					
				}else{
					dataTemp = data.data ;
				}
				for(var i=0;i<dataTemp.length;i++){
					config.selector.append(	eval('('+ config.reback+'(config.template ,dataTemp[ '+i+' ])' +')')  );
				}
			}
		}, function(){})
	}
}

function init_button(obj){
	var selectors = $('[type-option="'+obj+'"],.jui-'+obj) ;
	for(var i=0;i< selectors.length; i++ ){
		var config  = getConfig(selectors.eq(i)) ;
		selectors.eq(i).data("config",config);
		selectors.eq(i).click(function(){
			var config = $(this).data("config");
			if(typeof layer == 'undefined'){
				log("请先增加layer库");
			}
			if(config.type == 'wait'){
				operate_wait();
				eval('('+ config.callback+'(closeAll)' +')');
			}else if(config.type == 'confirm'){
				layer.confirm(gdv(config.content), {
					  btn: config.button_name ,
					  skin: gdv(config.skin),
					  shade: gdv(config.shade,0.3),
					}, function(){
						if(config.callback.length > 0){
							eval('('+ config.callback[0]+'()' +')');
						}
					}, function(){
						if(config.callback.length > 1){
							eval('('+ config.callback[1]+'()' +')');
						}
					});
			}else if(config.type == 'window' ){
				if( config.before != null && config.before !='' ){
					eval('('+ config.before +'()' +')');
				}
				openWindow(gdv(config.title,'Info'),config.selector,config.width);
			}else if(config.type == 'page'){
				layer.open({
					  type: 1,
					  title: gdv(config.title,'Info'),
					  skin:  gdv(config.skin), //样式类名
					  closeBtn: 1, 
					  anim: 2,
					  area: config.area,
					  shadeClose: true, 
					  shade: gdv(config.shade,0.3),
					  content: $(config.selector)
					});
			}else if(config.type == 'excel'){
				var paraent_objs =  $(config.selector).find("[type-name]") ;
				var param = gdv(config.param,"") ;
				for(var j=0; j< paraent_objs.length;j++){
					if( paraent_objs.eq(j).attr("type-value") ==  'h' ){
						if(!validater.empty(paraent_objs.eq(j).html()))
							param += "&" + paraent_objs.eq(j).attr("type-name")+"="+ paraent_objs.eq(j).html();			
					}else{
						if(!validater.empty(paraent_objs.eq(j).val()))
							param += "&" + paraent_objs.eq(j).attr("type-name")+"="+ paraent_objs.eq(j).val();	
					}
				}
				if(validater.empty(param)){
					window.open(localUrl+ config.url );		
				}else{
					window.open(localUrl+ config.url + '?'+ gdv(param,''));					
				}
			}
		})
	}
}

var config = [ "time" ,"simple_table", "table"  ,"search_table", "form_submit","detail_get" , "detail_post" 
  	,"validate","auto_complete","button","selecter","template","echart" ] ;


function table_operate_init(){
	$(".self-icon").each(function(i,ele){
		var content = $(this).attr("tag") ;
		$(this).webuiPopover({
			content: content ,
			placement:'top',
			trigger:'hover',
			style:'inverse',
			padding:false});
	})
}